// compare_string.rs

/// Compare two strings to determine if they are equal.
///
/// @param string1         The first string.
/// @param string2         The second string.
/// @return                Non-zero if the strings are equal, zero if they are
///                        not equal.
pub fn string_equal(string1: &str, string2: &str) -> bool {
    string1 == string2
}

/// Compare two strings.
///
/// @param string1         The first string.
/// @param string2         The second string.
/// @return                A negative value if the first string should be
///                        sorted before the second, a positive value if the
///                        first string should be sorted after the second,
///                        zero if the two strings are equal.
pub fn string_compare(string1: &str, string2: &str) -> i32 {
    match string1.cmp(string2) {
        std::cmp::Ordering::Less => -1,
        std::cmp::Ordering::Greater => 1,
        std::cmp::Ordering::Equal => 0,
    }
}

/// Compare two strings to determine if they are equal, ignoring the
/// case of letters.
///
/// @param string1         The first string.
/// @param string2         The second string.
/// @return                Non-zero if the strings are equal, zero if they are
///                        not equal.
pub fn string_nocase_equal(string1: &str, string2: &str) -> bool {
    string_nocase_compare(string1, string2) == 0
}

/// Compare two strings, ignoring the case of letters.
///
/// @param string1         The first string.
/// @param string2         The second string.
/// @return                A negative value if the first string should be
///                        sorted before the second, a positive value if the
///                        first string should be sorted after the second,
///                        zero if the two strings are equal.
pub fn string_nocase_compare(string1: &str, string2: &str) -> i32 {
    let mut chars1 = string1.chars().map(|c| c.to_ascii_lowercase());
    let mut chars2 = string2.chars().map(|c| c.to_ascii_lowercase());

    loop {
        match (chars1.next(), chars2.next()) {
            (Some(c1), Some(c2)) => {
                if c1 != c2 {
                    return if c1 < c2 { -1 } else { 1 };
                }
            }
            (None, None) => return 0,
            _ => return if string1.len() < string2.len() { -1 } else { 1 },
        }
    }
}
